﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Vds2465Telegrams
{
	public class UserData_Bytes : UserData
	{
		#region private member variables

		private byte[] _bytes;

		#endregion private member variables

		#region constructor

		public UserData_Bytes(byte dataType, byte[] bytes)
			: base(dataType)
		{
			if (bytes != null)
			{
				_bytes = new byte[bytes.Length];
				for (int index = 0; index < bytes.Length; index++)
				{
					_bytes[index] = bytes[index];
				}
			}
			else
			{
				// TODO handle error or warning - null bytes
				_bytes = new byte[0];
			}
		}	

		#endregion constructor

		#region getters

		public byte[] GetCopyOfBytes()
		{
			if (_bytes != null)
			{
				// we return a copy so that this object remains immutable
				byte[] retBytes = new byte[_bytes.Length];
				for (int index = 0; index < _bytes.Length; index++)
				{
					retBytes[index] = _bytes[index];
				}
				return retBytes;
			}

			return new byte[0];
		}

		#endregion getters

		#region ToString, Equals, GetHashCode

		public override int GetHashCode()
		{
			int hashCode = base.GetHashCode();
			hashCode ^= UserDataCommon.GetHashCode_ByteArray(_bytes);

			return hashCode;
		}

		public override string ToString()
		{
			StringBuilder stringBuilder = new StringBuilder();
			stringBuilder.Append("(");
			stringBuilder.Append(ToStringProperties());
			stringBuilder.Append(")");

			return stringBuilder.ToString();
		}

		internal override string ToStringProperties()
		{
			StringBuilder stringBuilder = new StringBuilder();
			stringBuilder.Append(base.ToStringProperties());

			stringBuilder.Append(", ");
			stringBuilder.Append(UserDataCommon.HexByteArrayToString("Bytes", _bytes));
						
			return stringBuilder.ToString();
		}

		public override bool Equals(object obj)
		{
			Type objType = obj.GetType();
			if (this.GetType() == objType)
			{
				UserData_Bytes userData2Compare = obj as UserData_Bytes;
				if (base.EqualsProperties(userData2Compare))
				{
					return EqualsProperties(userData2Compare);
				}
			}

			return false;
		}

		internal bool EqualsProperties(UserData_Bytes userData2Compare)
		{
			if (base.EqualsProperties(userData2Compare))
			{
				return UserDataCommon.Equals2ByteArrays(_bytes, userData2Compare.GetCopyOfBytes());
			}

			return false;
		}

		#endregion ToString, Equals, GetHashCode
	}
}
